<?php
/**
 * lsys orm
 * @author     Lonely <shan.liu@msn.com>
 * @copyright  (c) 2017 Lonely <shan.liu@msn.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0
 */
namespace LSYS\ORMCache\Cache;
use LSYS\ORM\Cache;
use LSYS\Config;
use LSYS\Config\SubSet;
use LSYS\ShareRedis;
use LSYS\ShareRedis\RedisShare;
class Redis extends Cache implements ShareRedis{
	use RedisShare;
	/**
	 * @var \Redis
	 */
	protected $_redis;
	/**
	 * @param Config $config
	 */
	public function __construct(Config $config=null){
		parent::__construct($config);
		$_config=$config->exist("config")?new SubSet($config, "config"):null;
		$this->_redis=self::get_share_redis($_config);
	}
	protected function _set_where($pk,$data,$timeout){
		return $this->_redis->setex('_L_O_W_'.$pk,$timeout,$data);
	}
	protected function _get_where($pk){
		return  $this->_redis->get('_L_O_W_'.$pk);
	}
	protected function _set_orm($pk,array $data,$timeout){
		$pk='_L_O_P_'.$pk;
		$s=$this->_redis->hmset($pk,$data);
		if ($s) $s=$this->_redis->expire($pk,$timeout);
		return $s;
	}
	protected function _get_orm($pk){
		$pk='_L_O_P_'.$pk;
		RETURN $this->_redis->hGetAll($pk);
	}
	protected function _exist_orm($pk){
		$pk='_L_O_P_'.$pk;
		return $this->_redis->exists($pk);
	}
	protected function _del_orm($pk){
		$pk='_L_O_P_'.$pk;
		return $this->_redis->del($pk);
	}
}